package com.huifaapp.mall.api;


import com.huifaapp.mall.base.bean.BaseBean;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.ObservableSource;
import io.reactivex.ObservableTransformer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Function;


public class RxJavaHelper {
//    /**
//     * 无进度Schedulers
//     */
//    public static <T> ObservableTransformer<T, T> applySchedulers() {
//        return new ObservableTransformer<T, T>() {
//            @Override
//            public ObservableSource<T> apply(@NonNull Observable<T> upstream) {
//                return upstream
//                        .subscribeOn(Schedulers.io()) //请求在IO线程
//                        .observeOn(AndroidSchedulers.mainThread());//
//
//            }
//        };
//    }

    /**
     * 处理数据.把BaseBean<T>转换成<T>  平指定指定线程
     *
     * @param <T>
     * @return
     */
    public static <T> ObservableTransformer<BaseBean<T>, T> handlResult() {
        return new ObservableTransformer<BaseBean<T>, T>() {
            @Override
            public ObservableSource<T> apply(Observable<BaseBean<T>> upstream) {
                return upstream.flatMap(new Function<BaseBean<T>, ObservableSource<T>>() {
                    @Override
                    public ObservableSource<T> apply(BaseBean<T> tBaseBean) throws Exception {
                        return createResobj(tBaseBean);
                    }
                }).subscribeOn(io.reactivex.schedulers.Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread());
            }
        };
    }

//    /**
//     * 有进度Schedulers
//     */
//    public static <T> ObservableTransformer<T, T> applySchedulers(@NonNull final Dialog dialog) {
//        return new ObservableTransformer<T, T>() {
//            @Override
//            public ObservableSource<T> apply(@NonNull Observable<T> upstream) {
//                return upstream
//                        .delay(1, TimeUnit.SECONDS)
//                        .subscribeOn(Schedulers.io())
//                        .doOnSubscribe(new Consumer<Disposable>() {
//                            @Override
//                            public void accept(@NonNull final Disposable disposable) throws Exception {
//                                dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
//                                    @Override
//                                    public void onCancel(DialogInterface dialog) {
//                                        disposable.dispose();
//                                    }
//                                });
//                                dialog.show();
//                            }
//                        })
//                        .observeOn(AndroidSchedulers.mainThread())
//                        .doOnTerminate(new Action() {
//                            @Override
//                            public void run() throws Exception {
//                                dialog.dismiss();
//                            }
//                        });
//            }
//        };
//    }

    /**
     * 根据code码 判断是否成功获取数据
     *
     * @param resobj
     * @param <T>
     * @return 成功返回数据 ,不成功返回自定义异常
     */
    private static <T> Observable<T> createResobj(final BaseBean<T> resobj) {

        return Observable.create(new ObservableOnSubscribe<T>() {
            @Override
            public void subscribe(ObservableEmitter<T> e) throws Exception {
                if (null == resobj) {
                    e.onError(new ApiException("", "数据为空!"));
                } else {
                    if (resobj.isSuccessful()) { //成功返回 数据
                        e.onNext(resobj.getData());
                    } else {   //失败返回 错误码和info
                        e.onError(new ApiException(resobj.getErrorCode(), resobj.getErrorMsg()));
                    }
                }


            }
        });
    }
}
